package com.controller;

import com.dto.BaseDto;
import com.entity.Buyer;
import com.page.PageInfo;
import com.service.BuyerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

@Controller
public class BuyerController {
    @Resource
    BuyerService buyerService;

//传统传参，不需要绑定参数 url直接在后面写上？buid=0001
    @RequestMapping("/buyer/getByBuidWithOrderInfosJson/{buid}")
    @ResponseBody
    public BaseDto getByBuidWithOrderInfosJson(@PathVariable("buid") String buid) {
        return buyerService.getByBuidWithOrderInfos(buid);
    }

    @RequestMapping("/buyer/getByBuidWithOrderInfos/{buid}")
    public ModelAndView getByBuidWithOrderInfos(@PathVariable("buid")String buid) {
        BaseDto dto =  buyerService.getByBuidWithOrderInfos(buid);
        return new ModelAndView("/front/buyer/buyerInfo","dto",dto);
    }


    @RequestMapping("/buyer/getByBuidJson")
    @ResponseBody
    public BaseDto getByBuidJson(String buid) {
        return buyerService.getBuyerByBuid(buid);
    }

//rest风格传参 将参数变为url的一部分但是需要绑定 例如 /buyer/getByBuidNew/0001
    @RequestMapping("/buyer/getByBuidNewJSon/{buid}")
    @ResponseBody
    public Buyer getByBuidNewJson(@PathVariable("buid") String buid) {
        return buyerService.getByBuid(buid);
    }

    @RequestMapping("/buyer/getAllJson")
    @ResponseBody
    public BaseDto<Buyer> getAllJson() {
        return buyerService.getAll();

    }

    @RequestMapping("/buyer/getAll")
    public ModelAndView getAll() {
       BaseDto<Buyer> dto = buyerService.getAll();
        return new ModelAndView("/back/buyer/list","dto",dto);

    }

    @RequestMapping("/buyer/getAllByPage")
    public ModelAndView getAllByPage(PageInfo pageInfo) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Buyer> dto = buyerService.getAllByPage(pageInfo);
        ModelAndView mv = new ModelAndView("/back/buyer/list","dto",dto);
        return mv;
    }
    @RequestMapping("/buyer/getAllByPageJson")
    @ResponseBody
    public BaseDto getAllByPageJson(PageInfo pageInfo) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Buyer> dto = buyerService.getAllByPage(pageInfo);
        return dto;
    }


    @RequestMapping("/buyer/searchNameByPage/{adname}")
    @ResponseBody
    public BaseDto searchNameByPage(PageInfo pageInfo,@PathVariable("adname")String adname) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Buyer> dto = buyerService.searchNameByPage(pageInfo,adname);
        return dto;
    }


    /**
     * 根据主键查询
     * @param buid
     * @return
     */
    @RequestMapping("/buyer/getBuyerByBuid/{buid}")
    @ResponseBody
    public BaseDto getBuyerByBuid(@PathVariable("buid") String buid){
        return buyerService.getBuyerByBuid(buid);
    }

    /**
     * 添加一个实体
     * @param buyer
     * @return
     */
    @RequestMapping("/buyer/addBuyer")
    @ResponseBody
    public BaseDto addBuyer(Buyer buyer){
        return buyerService.addBuyer(buyer);
    }
    /**
     * 修改实体
     */
    @RequestMapping("/buyer/updateBuyer")
    @ResponseBody
    public BaseDto updateBuyer(Buyer buyer){
        return buyerService.updateBuyer(buyer);
    }
    public BaseDto updateBuyerByExample(){
        return null;
    }

    /**
     * 删除一个实体
     * @param buid
     * @return
     */
    @RequestMapping("/buyer/deleteBuyerByPrimayKey/{buid}")
    @ResponseBody
    public BaseDto deleteBuyerByPrimayKey(@PathVariable("buid") String buid){
        return buyerService.deleteBuyerByPrimayKey(buid);
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public BaseDto deleteBuyerByExample(){
        return null;
    }

    @RequestMapping("/buyer/login")
    @ResponseBody
    public String login(String buid, HttpServletRequest request){
        request.getSession().setAttribute("buid",buid);
        return buid+"登录成功";
    }



}
